# -*- coding: utf-8 -*-
# 引入依赖包
# pip install alibabacloud_imagerecog20190930

import os
import io
from urllib.request import urlopen
from alibabacloud_imagerecog20190930.client import Client
from alibabacloud_imagerecog20190930.models import ClassifyingRubbishAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions

config = Config(
    # 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
    # 如果您用的是RAM用户的AccessKey，还需要为RAM用户授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
    # 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
    access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    # 访问的域名
    endpoint="imagerecog.cn-shanghai.aliyuncs.com",
    # 访问的域名对应的region
    region_id="cn-shanghai",
)
# 场景一：文件在本地
# img = open(r'/tmp/ClassifyingRubbish1.jpg', 'rb')
# 获取当前工作目录
current_dir = os.getcwd()
# 构建图片文件的完整路径
file_path = os.path.join(current_dir, 'ClassifyingRubbish1.jpg')
# 打开图片文件
img = open(file_path, 'rb')

classifying_rubbish_request = ClassifyingRubbishAdvanceRequest()
classifying_rubbish_request.image_urlobject = img
runtime = RuntimeOptions()
try:
    # 初始化Client
    client = Client(config)
    response = client.classifying_rubbish_advance(classifying_rubbish_request, runtime)
    # 获取整体结果
    print(response.body)
except Exception as error:
    # 获取整体报错信息
    print(error)
    # 获取单个字段
    print(error.code)
